package cn.xuchunh.campusbbs.ui.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.ui.activities.TopicDetailActivity;
import cn.xuchunh.campusbbs.adapter.TopicListAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Topic;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.net.params.TopicListParams;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;
import cn.xuchunh.campusbbs.utils.JsonKey;
import cn.xuchunh.campusbbs.viewholder.TopicViewHolder;

/**
 * Created by XuChunH on 2015/9/13.
 */
public class TopicListFragment extends BaseRefreshListFragment<TopicViewHolder, Topic> {

    public static final int TYPE_ALL = 0;
    public static final int TYPE_NEW = 1;
    public static final int TYPE_ESSENCE = 2;
    public static final int TYPE_TOP = 3;
    public static final int TYPE_MARROW = 4;

    private TopicListParams topicListParams = new TopicListParams();

    public TopicListFragment() {
    }

    public static TopicListFragment getInstance(int type, int forumId, int isImageList, int topOrder, String filterType) {
        TopicListFragment topicListFragment = new TopicListFragment();
        Bundle args = new Bundle();
        args.putInt(IntentParamsKey.TYPE, type);
        args.putInt(IntentParamsKey.FORUM_ID, forumId);
        args.putInt(IntentParamsKey.IS_IMAGE_LIST, isImageList);
        args.putInt(IntentParamsKey.TOP_ORDER, topOrder);
        args.putString(IntentParamsKey.FILTER_TYPE, filterType);
        topicListFragment.setArguments(args);
        return topicListFragment;
    }

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<TopicViewHolder, Topic> createAdapter() {
        return new TopicListAdapter(getActivity());
    }

    @Override
    protected List<Topic> parse(JSONObject jsonObject) throws JSONException {
        List<Topic> list = new ArrayList<>();
        JSONArray jsonArray = jsonObject.optJSONArray(JsonKey.LIST);
        int size = jsonArray.length();
        JSONObject topicJson;
        for (int i = 0; i < size; i++) {
            topicJson = jsonArray.optJSONObject(i);
            Topic topic = new Topic();
            topic.setForumId(topicJson.optInt(JsonKey.BOARD_ID));
            topic.setForumName(topicJson.optString(JsonKey.BOARD_NAME));
            topic.setId(topicJson.optInt(JsonKey.TOPIC_ID));
            topic.setType(topicJson.optString(JsonKey.TYPE));
            topic.setTitle(topicJson.optString(JsonKey.TITLE));
            topic.setUserId(topicJson.optInt(JsonKey.USER_ID));
            topic.setUserNickName(topicJson.optString(JsonKey.USER_NICK_NAME));
            String lastReplyDate = topicJson.optString(JsonKey.LAST_REPLY_DATE);
            topic.setLastReplyDate(TextUtils.isEmpty(lastReplyDate) ? 0 : Long.valueOf(lastReplyDate));
            topic.setVote(topicJson.optInt(JsonKey.VOTE));
            topic.setHot(topicJson.optInt(JsonKey.HOT) != 0);
            topic.setHits(topicJson.optInt(JsonKey.HITS));
            topic.setReplies(topicJson.optInt(JsonKey.REPLIES));
            topic.setEssence(topicJson.optInt(JsonKey.ESSENCE) != 0);
            topic.setTop(topicJson.optInt(JsonKey.TOP) != 0);
            topic.setStatus(topicJson.optInt(JsonKey.STATUS));
            topic.setSubject(topicJson.optString(JsonKey.SUBJECT));
            topic.setPicPath(topicJson.optString(JsonKey.PIC_PATH));
            topic.setRatio(topicJson.optString(JsonKey.RATIO));
            topic.setUserAvatar(topicJson.optString(JsonKey.USERAVATAR));
            topic.setGender(topicJson.optInt(JsonKey.GENDER));
            topic.setRecommendAdd(topicJson.optInt(JsonKey.RECOMMENDADD));
            topic.setIsHasRecommendAdd(topicJson.optInt(JsonKey.ISHASRECOMMENDADD));
            topic.setSourceWebUrl(topicJson.optString(JsonKey.SOURCEWEBURL));
            list.add(topic);
        }
        int hasNext = jsonObject.optInt(JsonKey.HAS_NEXT);
        getAdapter().setHasMore(hasNext != 0);
        return list;
    }

    @Override
    protected void loadDataFromServer() {
        topicListParams.setPage(getPage());
        HttpRequestManager.getInstance(AppContext.getInstance()).
                loadTopicList(topicListParams, new ListXJsonListener(), new SimpleErrorListener(), getVolleyTag());
    }

    @Override
    protected void loadDataFromCache() {

    }

    @Override
    public String getVolleyTag() {
        return Topic.class.getSimpleName();
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    public void handleArgs(Bundle args) {
        if (args != null) {
            topicListParams.setBoardId(args.getInt(IntentParamsKey.FORUM_ID));
            String[] sorts = getResources().getStringArray(R.array.topic_sort);
            topicListParams.setSort(sorts[args.getInt(IntentParamsKey.TYPE)]);
            topicListParams.setIsImageList(args.getInt(IntentParamsKey.IS_IMAGE_LIST, 0));
            topicListParams.setTopOrder(args.getInt(IntentParamsKey.TOP_ORDER, 0));
            topicListParams.setFilterType(args.getString(IntentParamsKey.FILTER_TYPE, ""));
        }
    }

    @Override
    public void onItemClick(int position, View item) {
        TopicDetailActivity.launch(getActivity(), getAdapter().getItem(position));
    }
}
